using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;

namespace Util.Config
{
    public class AppSettingConfig
    {
        #region 配置项

        /// <summary>
        /// 配置项
        /// </summary>
        public static IConfiguration configuration;

        #endregion

        #region 配置

        /// <summary>
        /// 初始化配置项
        /// </summary>
        static AppSettingConfig()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载            
            configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource
            {
                Path = "appsettings.json",
                ReloadOnChange = true
            })
            .Build();
        }

        #endregion

        #region 查询数据

        /// <summary>
        /// 获得指定键名的值
        /// </summary>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return configuration.GetSection(key).Value;
        }

        /// <summary>
        /// 获得指定键名下的数据库连接字串
        /// </summary>
        /// <param name="key">键名</param>
        /// <returns></returns>
        public static string GetConnectionString(string key)
        {
            return configuration.GetConnectionString(key);
        }

        /// <summary>
        /// 获得指定键的字符串：AppSetings下的键
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static string GetAppSeting(string key)
        {
            return GetValue($"AppSettings:{key}");
        }

        #endregion
    }
}
